import 'package:cotton_candy/components/button.dart';
import 'package:cotton_candy/components/cotton_candy_text.dart';
import 'package:cotton_candy/components/input.dart';
import 'package:cotton_candy/components/toast.dart';
import 'package:cotton_candy/notifier/user_notifier.dart';
import 'package:cotton_candy/services/user_services.dart';
import 'package:cotton_candy/utils/tip.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class LoginScreen extends StatelessWidget {
  final _userController = TextEditingController(text: '呆萌');
  final _passwordController = TextEditingController(text: '111111');
  @override
  Widget build(BuildContext context) {
    //亮色状态栏
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);

    return Scaffold(
      body: Container(
        margin: EdgeInsets.all(20),
        child: Column(
          children: [
            Container(
              child: CottonCandyText(),
              width: double.infinity,
              padding: EdgeInsets.fromLTRB(0, 100, 0, 0),
            ),
            Container(
              margin: EdgeInsets.only(bottom: 15, top: 50),
              child: Input(
                icon: CupertinoIcons.person_fill,
                controller: _userController,
                backgroundColor: Color.fromARGB(10, 0, 0, 0),
              ),
            ),
            Input(
              icon: CupertinoIcons.lock_fill,
              controller: _passwordController,
              isPassword: true,
              backgroundColor: Color.fromARGB(10, 0, 0, 0),
            ),
            Container(
              margin: EdgeInsets.only(top: 30),
              child: Button(
                child: Text(
                  '登 录',
                  style: TextStyle(fontSize: 18),
                ),
                onPressed: () async {
                  showToast(Container(
                    width: 80,
                    child: Row(
                      children: [
                        CupertinoActivityIndicator(
                          radius: 7,
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 8),
                          child: Text('登录中...'),
                        )
                      ],
                    ),
                  ));
                  try {
                    final a = await UserService.login(
                        username: _userController.text,
                        password: _passwordController.text);
                    await hideToast();
                    if (a.success) {
                      context.read<UserNotifier>().login(_userController.text);
                    } else {
                      tip(a.message);
                    }
                  } catch (e) {
                    hideToast();
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
